package com.yiqi.news.mvp.ui.activity

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.BottomSheetDialog
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import butterknife.OnClick
import com.chad.library.adapter.base.BaseQuickAdapter
import com.github.nukc.stateview.StateView
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils
import com.jess.arms.widget.DividerItemDecoration
import com.novel.cn.ext.dp2px
import com.yiqi.huize.isNull
import com.yiqi.news.R
import com.yiqi.news.app.service.UserController
import com.yiqi.news.app.widget.qmui.QMUITipDialog
import com.yiqi.news.di.component.DaggerVideoDetailComponent
import com.yiqi.news.di.module.VideoDetailModule
import com.yiqi.news.entity.NewsDetail
import com.yiqi.news.mvp.contract.VideoDetailContract
import com.yiqi.news.mvp.presenter.VideoDetailPresenter
import com.yiqi.news.mvp.ui.adapter.CommentAdapter
import com.yiqi.news.mvp.ui.holder.NewsDetailHeaderView
import kotlinx.android.synthetic.main.activity_video_detail.*
import kotlinx.android.synthetic.main.dialog_bottom_sheet_label.view.*
import kotlinx.android.synthetic.main.include_news_detail_tool_bar.*


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/19/2019 18:16
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
/**
 * 如果没presenter
 * 你可以这样写
 *
 * @ActivityScope(請注意命名空間) class NullObjectPresenterByActivity
 * @Inject constructor() : IPresenter {
 * override fun onStart() {
 * }
 *
 * override fun onDestroy() {
 * }
 * }
 */
class VideoDetailActivity : BaseActivity<VideoDetailPresenter>(), VideoDetailContract.View, TextView.OnEditorActionListener, BaseQuickAdapter.OnItemChildClickListener {
    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

    }

    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            ArmsUtils.hidekeyboard(this)
            if (mNewsDetail != null) {
                if (UserController.getInstance().isLogin && !mNewsDetail.isNull()) mPresenter?.doCommentNews(mNewsDetail?.id, mNewsDetail?.materialType, edt_news_comment.text.toString().trim()) else ArmsUtils.startActivity(LoginActivity::class.java)

            }
            return true
        }
        return false

    }

    private var isZan: Int = 0
    private var isCollect: Int = 0
    private lateinit var mView: View
    private lateinit var mNewsDetail: NewsDetail

    private val mBottomSheetDialog: BottomSheetDialog by lazy {
        return@lazy BottomSheetDialog(this)
    }

    private val mStateView: StateView by lazy {
        return@lazy StateView.inject(fl_content_video)
    }

    private val mNewsId: String by lazy {
        return@lazy intent.getStringExtra("news_id")
    }

    private val mHeaderView: NewsDetailHeaderView by lazy {
        return@lazy NewsDetailHeaderView(this)
    }


    private val mCommentAdapter: CommentAdapter by lazy {
        return@lazy CommentAdapter()
    }

    private val mQMUIInfoDialog: QMUITipDialog by lazy {
        return@lazy QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("发表评论成功")
                .create()
    }


    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerVideoDetailComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .videoDetailModule(VideoDetailModule(this))
                .build()
                .inject(this)
    }


    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_video_detail //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    override fun initEvent() {
        edt_news_comment.setOnEditorActionListener(this)
        initBottomSheetDialog()
    }

    override fun showNewsDetail(news: NewsDetail) {
        mNewsDetail = news
        setDetailData(mNewsDetail)
        mCommentAdapter.setNewData(mNewsDetail.materialCommentVoList)
        mHeaderView.setDetail(news) {
            //加载完成后，显示内容布局
            mStateView.showContent()
        }

    }

    private fun setDetailData(news: NewsDetail) {
        isZan = news.isZan
        isCollect = news.isCollect
        btn_news_like.setImageResource(if (isZan == 1) R.mipmap.ic_toolbar_news_like else R.mipmap.ic_toolbar_news_dislike)
        btn_news_collection.setImageResource(if (isCollect == 1) R.mipmap.ic_toolbar_collected else R.mipmap.ic_toolbar_news_collection)
    }

    override fun showNewsError(error: String) {
        mStateView.showRetry()
    }

    override fun showCommentSucess() {
        mPresenter?.getNewsData(mNewsId)
        rv_comment_video.scrollToPosition(0)
        mQMUIInfoDialog.show()
    }

    override fun showLikeSucess() {
        if (isZan == 0) {
            isZan = 1
            btn_news_like.setImageResource(R.mipmap.ic_toolbar_news_like)
        } else {
            isZan = 0
            btn_news_like.setImageResource(R.mipmap.ic_toolbar_news_dislike)
        }

    }

    override fun showCollectionSucess() {
        if (isCollect == 0) {
            isCollect = 1
            btn_news_collection.setImageResource(R.mipmap.ic_toolbar_collected)
        } else {
            isCollect = 0
            btn_news_collection.setImageResource(R.mipmap.ic_toolbar_news_collection)
        }
    }

    override fun showError(error: String) {
        showMessage(error)
    }

    override fun onStart() {
        super.onStart()
        mPresenter?.getNewsData(mNewsId)
    }


    override fun initData(savedInstanceState: Bundle?) {
        mStateView.setLoadingResource(R.layout.page_loading)
        mStateView.setRetryResource(R.layout.page_net_error)
        mStateView.showLoading()

        rv_comment_video.addItemDecoration(DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(dp2px(18)))
        mCommentAdapter.bindToRecyclerView(rv_comment_video)
        mCommentAdapter.setHeaderView(mHeaderView)
        mCommentAdapter.onItemChildClickListener = this


    }


    override fun showMessage(message: String) {
        ArmsUtils.snackbarText(message)
    }

    override fun launchActivity(intent: Intent) {
        ArmsUtils.startActivity(intent)
    }

    override fun killMyself() {
        finish()
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(R.id.toolbar).init()
    }


    private fun initBottomSheetDialog() {
        mView = LayoutInflater.from(this).inflate(R.layout.diaog_share, null, false)
        mBottomSheetDialog.setContentView(mView)
        (mView.parent as View).setBackgroundColor(resources.getColor(android.R.color.transparent))
        mView.tv_dialog_close.setOnClickListener { mBottomSheetDialog.dismiss() }
    }

    @OnClick(R.id.btn_news_like, R.id.btn_news_share, R.id.btn_news_collection)
    fun onClick(view: View) {
        when (view.id) {
            R.id.btn_news_like -> if (UserController.getInstance().isLogin && !mNewsDetail.isNull()) mPresenter?.doLikeNews(mNewsDetail.materialType, mNewsId) else ArmsUtils.startActivity(LoginActivity::class.java)
            R.id.btn_news_share -> mBottomSheetDialog.show()
            R.id.btn_news_collection -> if (UserController.getInstance().isLogin) mPresenter?.doNewsCollection(mNewsId) else ArmsUtils.startActivity(LoginActivity::class.java)
        }
    }
}
